package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.imooc.base.BaseInfoProperties;
import com.imooc.enums.YesOrNo;
import com.imooc.exceptions.GraceException;
import com.imooc.grace.result.ResponseStatusEnum;
import com.imooc.mapper.DataDictionaryMapper;
import com.imooc.pojo.DataDictionary;
import com.imooc.pojo.bo.DataDictionaryBO;
import com.imooc.service.DataDictionaryService;
import com.imooc.utils.PagedGridResult;
import javafx.scene.Group;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Author Mr.Yang
 * @Date 2024-08-30 10:07
 * @question:
 */

@Service
public class DataDictionaryServiceImpl extends BaseInfoProperties implements DataDictionaryService {

    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;

    @Transactional
    @Override
    public void createOrUpdateDataDictionary(DataDictionaryBO dataDictionaryBO) {

        DataDictionary dataDictionary = new DataDictionary();
        BeanUtils.copyProperties(dataDictionaryBO, dataDictionary);

        //如果id为空 则新增新的数据字典项
        if (StringUtils.isBlank(dataDictionaryBO.getId())) {

            //字典项不可重复
            isDictExist(dataDictionaryBO);

            dataDictionaryMapper.insert(dataDictionary);
        } else {

            //字典项不可重复
            isDictExist(dataDictionaryBO);

            //否则id不为空 则修改数据字典项
            dataDictionaryMapper.updateById(dataDictionary);
        }
    }

    /**
     * 判断字典项是否重复
     * @param dataDictionaryBO
     */
    private void isDictExist(DataDictionaryBO dataDictionaryBO) {
        DataDictionary ddExist =  dataDictionaryMapper.selectOne(
                new QueryWrapper<DataDictionary>()
                        .eq("item_key", dataDictionaryBO.getItemKey())
                        .eq("item_value", dataDictionaryBO.getItemValue())
        );
        if (ddExist != null) {
            GraceException.display(ResponseStatusEnum.DATA_DICT_EXIST_ERROR);
        }
    }

    @Override
    public PagedGridResult getDataDictListPaged(String typeName, String itemValue, Integer page, Integer limit) {

        PageHelper.startPage(page, limit);

        List<DataDictionary> ddList = dataDictionaryMapper.selectList(
                new QueryWrapper<DataDictionary>()
                        .like("type_name", typeName)
                        .like("item_value", itemValue)
                        .orderByAsc("type_code")
                        .orderByAsc("sort")
        );

        return setterPagedGrid(ddList, page);
    }

    @Override
    public DataDictionary getDataDictionary(String dictId) {
        return dataDictionaryMapper.selectById(dictId);
    }

    @Transactional
    @Override
    public void deleteDataDictionary(String dictId) {
        int res = dataDictionaryMapper.deleteById(dictId);
        if (res == 0 ) GraceException.display(ResponseStatusEnum.DATA_DICT_DELETE_ERROR);
    }

    @Override
    public List<DataDictionary> getDataByCode(String typeCode) {

        List<DataDictionary> ddList = dataDictionaryMapper.selectList(
                new QueryWrapper<DataDictionary>()
                        .eq("type_code", typeCode)
                        .eq("enable", YesOrNo.YES.type)
                        .orderByAsc("sort")
        );

        return ddList;
    }

    @Override
    public Map<String, List<DataDictionary>> getDictList() {

        List<DataDictionary> dataDictionaryList = dataDictionaryMapper.selectList(null);
        Map<String, List<DataDictionary>> dataDictByTypeCode = dataDictionaryList.stream().collect(Collectors.groupingBy(DataDictionary::getTypeCode));

        return dataDictByTypeCode;
    }


}

